<?php

// +----------------------------------------------------------------------
 // | 文件 FeedBackController.class.php
 // +----------------------------------------------------------------------
 // | 说明 意见反馈
 // +----------------------------------------------------------------------
 // | 
 // +----------------------------------------------------------------------
 // | Author: 谢赤心 Legend. <xcx_legender@qq.com>
 // +----------------------------------------------------------------------
  

namespace Home\Controller;

use Common\Util\Validate;

class FeedbackController extends HomeController 
{	

	public function pc()
	{
		if (IS_POST){
		
			if (!I('phone')){
				$this->error('手机号码不能为空');
			}
			$model = service('Feedback');
			$data = [
				'phone' 	=> I('phone'),
				'title' 	=> I('title'),
				'content' 	=> I('content'),
				'platform' 	=> I('platform'),
				'type' 	    => I('type'),
				'uid' 		=> UID,
			];
			if ($model->create($data) && $model->add()){
				return $this->success('意见提交成功');
			}
 
			return $this->error($model->getError());


		}else{
			$this->assign('uid', UID);
			$this->display();
		}
	}

	public function web()
	{
		if (!UID) {
        	$this->ajaxReturn(array('status'=>0, 'info'=>'未登录'));
        }
        $last_feedback_time = session('LAST_FEEDBACK_TIME');
        $verify = I('verify', '', 'trim');
        if (!empty($last_feedback_time) && time() - $last_feedback_time < 300) {
        	if (empty($verify)) {
	        	$this->ajaxReturn(['state' => -1, 'info' => '输入验证码', 'url' => C('LOCAL_DOMAIN').'/user/verify']);
        	} elseif (!check_verify($verify)) {
	        	$this->ajaxReturn(['state' => -3, 'info' => '验证码错误', 'url' => C('LOCAL_DOMAIN').'/user/verify']);
        	}
        }
		$content = I('content', '', 'trim,htmlspecialchars');
		if (!$content) {
			$this->ajaxReturn(['state' => -2, 'info'=> '内容不能为空']);
		}
		$res = event('Feedback')->add(UID, '', $content, 0);
		if ($res) {
			session('LAST_FEEDBACK_TIME', time());
			$this->ajaxReturn(['state' => 0, 'info'=> '提交成功']);
		} else {
			$this->ajaxReturn(['state' => -2, 'info'=> '提交失败']);
		}
	}

}
